package ui;

import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

import exceptions.*;

import appControll.AppController;

import java.text.*;
import java.util.*;

import model.*;


public class CommandeTableModel extends AbstractTableModel{
	private ArrayList<Commande> content = new ArrayList<Commande>();
	private ArrayList<String> columnNames =  new ArrayList<String>();
	private AppController app = new AppController();
	private int type;
	
	public CommandeTableModel(int type, int annee, int section, ArrayList<String> etats) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		this();
		this.type = type;
		content = (ArrayList<Commande>)app.getAllCommande(this.type, annee, section, etats);
	}
	public CommandeTableModel(){
		columnNames.add("Date commande");
		columnNames.add("Client");
		columnNames.add("Etat");
		columnNames.add("Prix");
		columnNames.add("Paye");
	}
	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return columnNames.size();
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return content.size();
	}
	public String getColumnName(int col){
		return columnNames.get(col);
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int row, int col) {
		Commande com = content.get(row);
		DateFormat df = DateFormat.getDateInstance();
		NumberFormat nf = NumberFormat.getCurrencyInstance();
		switch(col){
		case 0: return df.format(com.getDate_commande().getTime());
		case 1: return com.getClient();
		case 2: return com.getEtat();
		case 3: try {
			return nf.format(app.getPrixCommande(com.getContenu()));
		} catch (PasIdentificationException e) {
			JOptionPane.showMessageDialog(null, e, "Erreur", JOptionPane.ERROR_MESSAGE);
		} catch (DatabaseErrorException e) {
			JOptionPane.showMessageDialog(null, e, "Erreur", JOptionPane.ERROR_MESSAGE);
		} catch (InvalidDataException e) {
			
		}
		case 4: return ((com.isPaye())?"Oui":"Non");
		default : return null;
		}
	}
	public Class getColumClass(int col){
		Class c;
		switch(col){
		case 0: c=Date.class;
				break;
		case 3: c=Double.class;
				break;
		default: c= String.class;
		}
		return c;
	}
	public Commande getElementAt(int index){
		return content.get(index);
	}
}